package com.crl.ui.controls.pages
{
	import com.crl.core.IFactory;
	import com.crl.ui.controls.listClass.ChooseState;
	import com.crl.ui.controls.listClass.IListItemRenderer;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PageList extends Sprite
	{
		private var caches:Array;
		//元素构造器
		protected var itemRenderFactory : IFactory;
		protected var _currentSelectItem:IListItemRenderer;
		
		protected var rowHeight : uint;
		protected var columnWidth : uint;
		private var train:Boolean;
		private var columnCount : uint;
		private var space : uint;
		
		/**
		 * 构造函数
		 * @param item 必须，需要加载的单元容器对象
		 * @param columnWidth 必须，单元之间的宽度
		 * @param rowHeight 必须，单元之间的高度
		 * @param train 排列方式，true 为横向排列,false 为纵向排列 ，默认为 true
		 * @param columnCount 每行\列最多排列数，默认为 1
		 * @param space 列表头空格数，必须非负，默认为 0
		 * @return 
		 * 
		 */		
		public function PageList(itemFacotry:IFactory,columnWidth:uint,rowHeight:uint,train:Boolean = true,columnCount:uint = 1,space:uint = 0){
			this.itemRenderFactory = itemFacotry;
			this.rowHeight = rowHeight;
			this.columnWidth = columnWidth;
			this.train = train;
			this.columnCount = columnCount;
			this.space = space;
			
			caches=[];
		}
		
		
		public function displayList(data:Array):void{
			var item:IListItemRenderer;
			var len:int=data.length;
			
			clear();
			
			for(var i:int=0;i<len;i++){
				
				item=caches[i];
				if(item==null){
					item=itemRenderFactory.newInstance();
					caches[i]=item;
				}
				item.chooseState=ChooseState.NORMAL;
				
				item.data=data[i];
				
				if( item is InteractiveObject )
				{
					var dis:InteractiveObject=item as InteractiveObject;
					bindItemEvent(dis,true);
					arrange(dis,i);
					this.addChild(dis);
				}
			}
			
		}
		
		protected function bindItemEvent(item:InteractiveObject,isBind:Boolean):void{
			if(item==null)return;
			
			if(isBind){
				//item.doubleClickEnabled=true;
				item.addEventListener(MouseEvent.CLICK,onChooseItem,false,0,true);
				//item.addEventListener(MouseEvent.DOUBLE_CLICK,onChooseItem,false,0,true);
				//item.addEventListener(MouseEvent.ROLL_OVER,rollHandler,false,0,true);
				//item.addEventListener(MouseEvent.ROLL_OUT,rollHandler,false,0,true);
			}else{
				item.removeEventListener(MouseEvent.CLICK,onChooseItem);
				//item.removeEventListener(MouseEvent.DOUBLE_CLICK,onChooseItem);
				//item.removeEventListener(MouseEvent.ROLL_OVER,rollHandle);
				//item.removeEventListener(MouseEvent.ROLL_OUT,rollHandler);
			}
		}
		
		private function onChooseItem(event:MouseEvent):void{
			var item:IListItemRenderer=event.currentTarget as IListItemRenderer;
			chooseItem(item);
			dispatchEvent(event);
		}
		
		private var _repeat:Boolean=false;
		public function set allowRepeat(repeat:Boolean):void{
			_repeat=repeat;
		}
		
		protected function chooseItem(item:IListItemRenderer):void{
			if(_currentSelectItem ==item && _repeat==false){
				return;
			}
			if(_currentSelectItem != null){
				_currentSelectItem.chooseState = ChooseState.NORMAL;
			}
			
			_currentSelectItem = item;
			if(_currentSelectItem){
				_currentSelectItem.chooseState = ChooseState.SELECT;
			}
			
			this.dispatchEvent(new Event(Event.SELECT));
		}
		
		public function get selectItem():IListItemRenderer{
			return this._currentSelectItem;
		}
		
		public function clear():void{
			for each(var item:IListItemRenderer in caches){
				bindItemEvent(item as InteractiveObject,false);
			}
			while(this.numChildren){
				this.removeChildAt(0);
			}
		}
		
		public function dispose():void{
			this.clear();
			this.caches=null;
		}
		
		/** 调整位置 */
		protected function arrange(_displayItem:DisplayObject,i:uint):void{
			var _w:int = this.columnWidth;
			var _h:int = this.rowHeight;
			var _l:int = this.columnCount;
			
			if(train){ 
				//横向排列
				if (_l>1)
				{
					_displayItem.x = (i%_l)*_w;
					_displayItem.y = Math.floor(i/_l)*_h;
				}else
				{
					_displayItem.x = 0;
					_displayItem.y = _h*i;
				}
			}else{ 
				//纵向排列
				if (_l>1)
				{
					_displayItem.x = Math.floor(i/_l)*_w;
					_displayItem.y = (i%_l)*_h;
				}else
				{
					_displayItem.x = _w*i;
					_displayItem.y = 0;
				}
			}
			
		}
		
		
		
	}
}